#region Using Statements
using System;
using System.IO;
using System.Collections;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
#endregion

namespace SkyBurner
{
    public class SoundManager
    {
        protected BaseGame m_Game;
        protected AudioEngine m_audioEngine;
        protected WaveBank m_waveBank;
        protected SoundBank m_soundBank;
        protected AudioCategory m_musicCategory;
        protected bool m_bEnabled;
        protected Cue m_cueBackgroundMusic;
        protected String m_backgroundMusic;

        public bool Enabled
        {
            get { return m_bEnabled; }
            set { m_bEnabled = value; }
        }

        public SoundManager(BaseGame pGame)
        {
            m_bEnabled = true;
            m_Game = pGame;
        }

        public void Initialize(String pSoundPath, String pSoundFile)
        {
            m_audioEngine = new AudioEngine(Path.Combine(pSoundPath, pSoundFile));
            m_waveBank = new WaveBank(m_audioEngine, Path.Combine(pSoundPath, "Wave Bank.xwb"));

            if (m_waveBank != null)
                m_soundBank = new SoundBank(m_audioEngine, Path.Combine(pSoundPath, "Sound Bank.xsb"));

            m_musicCategory = m_audioEngine.GetCategory("Music");
        }

        public void Update()
        {
            if (m_bEnabled && (m_audioEngine != null))
            {
                m_audioEngine.Update();
                if ((m_cueBackgroundMusic != null) && !m_cueBackgroundMusic.IsPlaying)
                    PlayBackgroundMusic(m_backgroundMusic);

            }
        }

        public void PlayBackgroundMusic(String pMusicTheme)
        {
            if (!m_bEnabled || (m_audioEngine == null))
                return;

            m_backgroundMusic = pMusicTheme;
            m_cueBackgroundMusic = m_soundBank.GetCue(m_backgroundMusic);
            if (m_cueBackgroundMusic != null)
                m_cueBackgroundMusic.Play();
        }

        public void PlaySound(String pSoundName)
        {
            if (!m_bEnabled || (m_audioEngine == null))
                return;

            try
            {
                if (m_soundBank.GetCue(pSoundName).IsPlaying)
                    return;
               
                m_soundBank.PlayCue(pSoundName);                
            }
            catch (Exception e)
            {
                m_Game.Console.Print("SoundManager.PlaySound : " + e.Message);
            }
        }

        public void StopBackgroundMusic()
        {
            m_musicCategory.Stop(AudioStopOptions.Immediate);
        }
    }
}
